/**
 * Created by hubo on 2016/4/20.
 */
$(function() {
    $("#addForm").validate({
        focusCleanup : true,
        focusInvalid : false,
        onkeyup : true,
         ignore : "",// 验证隐藏域，解决资源列表以及角色名称隐藏域不予验证的问题
        rules : {
            method : {
                required : true
            },
            methodName:{
                required:true
            },
            limitD:{
                number:true
            },
            limitH:{
                number:true
            },limitM:{
                number:true
            }
            ,limitS:{
                number:true
            }


        },
        submitHandler : submit
    });

    $.ajax({
        url:ctx+"/service/queryAll",
        type:'post',
        data:"",
        dataType:'json',
        timeout:ajaxTimeout,
        success:function(json){
            var dataJson={};
            dataJson.value= json.result;
            $("input#method").bsSuggest({
                data:dataJson,
                getDataMethod:"data",
                effectiveFields:["method"],
                effectiveFieldsAlias: {"method":"请选择接口"},
                showBtn: false,
                showHeader: false,
                idField: "method",
                keyField: "method",
                autoMinWidth: false,            //是否自动最小宽度，设为 true 则最小宽度不小于输入框宽度
                listAlign: "right"              //提示列表对齐位置，left/right/auto
            }).on('onSetSelectValue', function (e, keyword, data) {
                $("#method").val(keyword.id);
            });
        }
    });

    $("#method").blur(function(){
        var method=$("#method").val();
        $.ajax({
            url:ctx+"/service/getByMethod/"+method,
            type:'post',
            data:"",
            dataType:'json',
            success:function(json){
                $("#methodName").val(json.result.memo);
            }
        });
    });
});


//获取url参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

/**
 * 表单提交
 */
function submit() {
    var partner = getQueryString("partnerHidden");
    $.ajax({
        url : ctx + '/developer/addPermission?partner='+partner,
        type : 'post',
        dataType : 'json',
        timeout : ajaxTimeout,
        data : $('#addForm').serialize(),
        error : function(json) {
            toastr.error("添加失败");
        },
        success : function(json) {
            if(json.returnCode=='T'){
                toastr.success(json.returnMsg);
            }else{
                toastr.error(json.returnMsg)
            }
        }
    });
}

$("#goBack").click(function(){
    var partner = getQueryString("partnerHidden");
    location.href=ctx+"/send/developer/interfaceList?partner="+partner;
});